#include "baseInfo.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <uuid/uuid.h>
#include <string>
#include "publicFunc.h"
#include <iostream>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include<sys/unistd.h>
using namespace std;

// 获取系统安装时间
void SystemCreateTime(char *cTime, int nLen)
{
    if(cTime == NULL || nLen <= 0)
        return;
    system("sudo dumpe2fs /dev/sda1 | grep \"Filesystem created\" > insdate");

    char * filename = "./insdate";

    FILE *fp = NULL;
    fp = fopen(filename, "r");
    if(fp == NULL)
    {
        perror("open file failed");
        return;
    }

        char buf[1024];
    memset(buf, 0, sizeof(buf));

    char year[16];
    char month[16];
    char day[16];
    char time[16];
   
    char field[256];
    memset(field, 0, sizeof(field));
    int n = 0;
    int nt = 0;
    if(fgets(buf, 1024-1, fp) != NULL)
    {
        int len = strlen(buf);
        for(int i = 0; i < len; i++)
        {
            if(buf[i] != ' ' && buf[i] != '\n')
            {
                field[n] = buf[i];
                n++;
            }
            else if(buf[i] == ' ' && buf[i-1] == ' ')
                continue;
            else
            {
                switch(nt)
                {
                    case 3:
                    {
                        //cout << field << endl;
                        memset(month, 0, sizeof(month));
                        strncpy(month, field, 16-1);
                        break;
                    }
                    case 4:
                    {
                        //cout << field << endl;
                        memset(day, 0, sizeof(day));
                        strncpy(day, field, 16-1);
                        break;
                    }
                    case 5:
                    {
                        //cout << field << endl;
                        memset(time, 0, sizeof(time));
                        strncpy(time, field, 16-1);
                        break;
                    }
                    case 6:
                    {
                        //cout << field << endl;
                        memset(year, 0, sizeof(year));
                        strncpy(year, field, 16-1);
                        break;
                    }
                }
                memset(field, 0, sizeof(field));
                n = 0;
                nt++;
            }
        }
    }

    fclose(fp);
    system("rm insdate");

    int mon = 0;
    if(strcmp(month, "Jan") == 0)
    {
        mon = 1;
    }
    else if(strcmp(month, "Feb") == 0)
    {
        mon = 2;
    }
    else if(strcmp(month, "Mar") == 0)
    {
        mon = 3;
    }
    else if(strcmp(month, "Apr") == 0)
    {
        mon = 4;
    }
    else if(strcmp(month, "May") == 0)
    {
        mon = 5;
    }
    else if(strcmp(month, "Jun") == 0)
    {
        mon = 6;
    }
    else if(strcmp(month, "Jul") == 0)
    {
        mon = 7;
    }
    else if(strcmp(month, "Aug") == 0)
    {
        mon = 8;
    }
    else if(strcmp(month, "Sep") == 0)
    {
        mon = 9;
    }
    else if(strcmp(month, "Oct") == 0)
    {
        mon = 10;
    }
    else if(strcmp(month, "Nov") == 0)
    {
        mon = 11;
    }
    else if(strcmp(month, "Dec") == 0)
    {
        mon = 12;
    }

    int nDay = atoi(day);
    char date[64];
    memset(date, 0, sizeof(date));
    snprintf(date, 64-1, "%s-%02d-%02d %s", year, mon, nDay, time);
    //cout << "系统安装时间：" << date << endl;

    strncpy(cTime, date, nLen-1);

    return;

}

// 获取系统当前时间
void SystemCurrentTime(char *cTime, int nLen)
{
    if(cTime == NULL || nLen <= 0)
        return;
/*        
    system("date > date");

    char *filename = "./date";
    FILE *fp = NULL;
    fp = fopen(filename, "r");
    if(fp == NULL)
    {
        perror("open file failed");
        return;
    }

    int n = 0;
    int nt = 0;
    char field[64];
    memset(field, 0, sizeof(field));

    char year[5];
    char month[3];
    char day[3];
    char time[16];

    char buf[1024];
    memset(buf, 0, sizeof(buf));
    if(fgets(buf, 1024-1, fp) != NULL)
    {
        int len = strlen(buf);
        for(int i = 0; i < len; i++)
        {
            if(buf[i] != ' ' && buf[i] != '\n')
            {
                field[n] = buf[i];
                n++;
            }
            else if(buf[i] == ' ' && buf[i-1] == ' ')
                continue;
            else
            {
                switch(nt)
                {
                    case 0:
                    {
                        //cout << field << endl;
                        memset(year, 0, sizeof(year));
                        strncpy(year, field, 5-1);
                        break;
                    }
                    case 1:
                    {
                        //cout << field << endl;
                        memset(month, 0, sizeof(month));
                        strncpy(month, field, 3-1);
                        break;
                    }
                    case 2:
                    {
                        //cout << field << endl;
                        memset(day, 0, sizeof(day));
                        strncpy(day, field, 3-1);
                        break;
                    }
                    case 4:
                    {
                        //cout << field << endl;
                        memset(time, 0, sizeof(time));
                        strncpy(time, field, 16-1);
                        break;
                    }
                }
                memset(field, 0, sizeof(field));
                n = 0;
                nt++;
            }
        }
    }
    fclose(fp);
    system("rm date");
*/

    long ti;
    struct tm *tm;

    time(&ti);
    tm=localtime(&ti);



    char date[64];
    memset(date, 0, sizeof(date));
    snprintf(date, 64-1, "%04d-%02d-%02d %02d:%02d:%02d", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
    //cout <<  "当前时间：" << date << endl;

    strncpy(cTime, date, nLen-1);

    return;
}

// 获取内存大小
void SystemMemory(char *cMem, int nLen)
{
    if(cMem == NULL || nLen <= 0)
        return;
    system("free -m > mem");

    char *fileName = "./mem";
    FILE *fp = NULL;
    fp = fopen(fileName, "r");
    if(fp == NULL)
    {
        perror("open file failed");
        return;
    }

    char buf[1024];
    memset(buf, 0, sizeof(buf));
    int nLine = 0;
    int n = 0;
    int nt = 0;
    char field[64];
    while(fgets(buf, 1024-1, fp) != NULL)
    {
        if(nLine == 1)
        {
            int len = strlen(buf);
            for(int i = 0; i < len; i++)
            {
                if(buf[i] != ' ' && buf[i] != '\n')
                {
                    field[n] = buf[i];
                    n++;
                }
                else if(buf[i] == ' ' && buf[i-1] == ' ')
                    continue;
                else
                {
                    if(nt == 1)
                    {
                        //cout << field << endl;
                        break;
                    }
                    memset(field, 0, sizeof(field));
                    n = 0;
                    nt++;
                }
            }
        }
        memset(buf, 0, sizeof(buf));
        nLine++;
    }

    fclose(fp);
    system("rm mem");
    //cout << "内存：" << field << "MB" << endl;

    strncpy(cMem, field, nLen-1);
}

// 硬盘大小
void DiskInfo(char *cDisk, int nLen)
{
    if(cDisk == NULL || nLen <= 0)
        return;
    system("sudo fdisk -l > disk");

    char *fileName = "./disk";
    FILE *fp = NULL;
    fp = fopen(fileName, "r");
    if(fp == NULL)
    {
        perror("open file failed");
        return;
    }

    char buf[1024];
    memset(buf, 0, sizeof(buf));
    while(fgets(buf, 1024-1, fp) != NULL)
    {
        if(strFind(buf, "/dev/sda:") >= 0)
        {
            //cout << buf << endl;
            string left = StrLeft(buf, strFind(buf, ","));
            //cout << left << endl;
            string disk = strMid(left.c_str(), strFind(left.c_str(), ": ")+1);
            //cout << disk << endl;
            strncpy(cDisk, disk.c_str(), nLen);
            break;
        }
        memset(buf, 0, sizeof(char)*1024);
    }
    if(strlen(cDisk) <= 0)
    {
        fseek(fp, 0, SEEK_SET);
        while(fgets(buf, 1024-1, fp) != NULL)
        {
            if(strFind(buf, "/dev/sda：") >= 0)
            {
                //cout << buf << endl;
                string left = StrLeft(buf, strFind(buf, ","));
                //cout << left << endl;
                string disk = strMid(left.c_str(), strFind(left.c_str(), "：")+2);
                //cout << disk << endl;
                strncpy(cDisk, disk.c_str(), nLen);
                break;
            }
            memset(buf, 0, sizeof(char)*1024);
        }
    }

    system("rm disk");
    fclose(fp);
}

// 分区信息
void PartitionInfo(char *info, int nLen)
{
    if(info == NULL)
        return;

    system("df >partinfo");
    char *fileName = "partinfo";

    FILE *fp = NULL;
    fp = fopen(fileName, "r");
    if(fp == NULL)
    {
        perror("open partinfo file failed");
        return;
    }

    char buf[1024];
    memset(buf, 0, sizeof(buf));

    int len = 0;
    char field[256];

    int size = 0;
    while(fgets(buf, 1024-1, fp) != NULL)
    {
        len = strlen(buf);
        int j = 0;
        int m = 0;
        for(int i = 0; i < len; i++)
        {
            if((j == 5 || buf[i] != ' ') && buf[i] != '\n')
            {
                field[m] = buf[i];
                if(m < 255)
                    m++;
            }
            else if(buf[i] == ' ' && buf[i-1] == ' ')
                continue;
            else
            {
                switch(j)
                {
                    case 0:
                    {
                        //cout << field << endl;
                        size += strlen(field)+1;
                        if(size < nLen)
                        {
                            strcat(info, field);
                            strcat(info, ",");
                        }
                        break;
                    }
                    case 1:
                    {
                        //cout << field << endl;
                        size += strlen(field)+1;
                        if(size < nLen)
                        {
                            strcat(info, field);
                            strcat(info, ",");
                        }
                        break;
                    }
                    case 2:
                    {
                        //cout << field << endl;
                        size += strlen(field)+1;
                        if(size < nLen)
                        {
                            strcat(info, field);
                            strcat(info, ",");
                        }
                        break;
                    }
                    case 3:
                    {
                        //cout << field << endl;
                        size += strlen(field)+1;
                        if(size < nLen)
                        {
                            strcat(info, field);
                            strcat(info, ",");
                        }
                        break;
                    }
                    case 4:
                    {
                        //cout << field << endl;
                        size += strlen(field)+1;
                        if(size < nLen)
                        {
                            strcat(info, field);
                            strcat(info, ",");
                        }
                        break;
                    }
                    case 5:
                    {
                        //cout << field << endl;
                        size += strlen(field)+1;
                        if(size < nLen)
                        {
                            strcat(info, field);
                            strcat(info, ";");
                        }
                        break;
                    }
                }
                memset(field, 0, sizeof(field));
				m = 0;
				j++;
            }
            
        }

    }

    fclose(fp);
    system("rm partinfo");

    return;
}
void GetUnixSocket(sqlite3 *db, char *Mac, char *eventId)
{
   // strstr
    system(" sudo netstat -anp >anp");
    char *fileName = "anp";

    FILE *fp = NULL;
    fp = fopen(fileName, "r");
    if(fp == NULL)
    {
        perror("open netstat file failed");
        return;
    }

    char buf[1024];
    memset(buf, 0, sizeof(buf));

    int len = 0;
	char field[256];
	memset(field,0,256);
	char proc[36];
	memset(proc,0,36);
	char RefCnt[36];
	memset(RefCnt,0,36);
	char Flags[36];
	memset(Flags,0,36);
	char Type[50];
	memset(Type,0,50);

	char state[20];
	memset(state,0,20);
	char pid_program_name[256];
	memset(pid_program_name,0,256);

	char inode[256];
	memset(inode,0,256);
	char path[256];
	memset(path,0,256);
	int nLen=4000;
    int size = 0;
    int index=0;
    int Index=0;
  	while(fgets(buf, 1024-1, fp) != NULL)
	{
       // cout<<buf<<endl;
       // cout<<"活跃的UNIX域套接字"<<endl;
        int ll=0;
		int l=0;
        if(strstr(buf,"活跃的UNIX域套接字")!=NULL)
        {
           
            index++;

            
        }
        if(strstr(buf,"Active UNIX domain sockets")!=NULL)
		{
			index++;
		}
        if(index>0)
        {
            Index++;
        }
        if(Index>2)
        {
           // cout<<Index<<endl;
			len = strlen(buf);
			
			int j = 0;
			int m = 0;
			for(int i = 0; i < len; i++)
			{
				if((j == 10 || buf[i] != ' ') && buf[i] != '\n')
				{
					field[m] = buf[i];
					if(m < 255)
						m++;
				}
				else if(buf[i] == ' ' && buf[i-1] == ' ')
				{
					ll++;
					continue;
				}
				else
				{
					switch(j)
					{
					case 0:
						{
							//cout << field << endl;
							size += strlen(field)+1;
							if(size < nLen)
							{
								strcpy(proc,field);
								//strcat(info, field);
								//strcat(info, ",");
							}
							break;
						}
					case 1:
						{
							//cout << field << endl;
							size += strlen(field)+1;
							if(size < nLen)
							{
								strcpy(RefCnt,field);
							
							}
							break;
						}
					case 2:
						{
							//cout << field << endl;
							size += strlen(field)+1;
							if(size < nLen)
							{
								strcpy(Flags,field);
							
							}
							break;
						}
					case 3:
						{
							//cout << field << endl;
							size += strlen(field)+1;
							if(size < nLen)
							{
								if(strstr(field,"]")!=NULL)
								{
									l++;
									strcat(Flags,field);
								}
								else
								{
									strcat(Flags,field);
								}
								
							
							}
							break;
						}
					case 4:
						{
							//cout << field << endl;
							size += strlen(field)+1;
							if(size < nLen)
							{
								if(l!=0)
								{
									strcpy(Type,field);
								}
								else
								{
									strcat(Flags,field);
								}
								
								
							}
							break;
						}
					
					case 5:
						{
							//cout << field << endl;
							
							size += strlen(field)+1;
							if(size < nLen)
							{
								if(ll>28)
								{
									strcpy(inode,field);
								}
								else
								{
									if(l!=0)
										
									{
										strcpy(state,field);
									}
									else
									{
										strcpy(Type,field);
									}
									
								}
								
							
							}
							break;
						}
					case 6:
						{
							//cout << field << endl;

							size += strlen(field)+1;
							if(size < nLen)
							{
								if(ll>31)
								{
									strcpy(pid_program_name,field);
								}
								else
								{
									strcpy(state,field);
									//strcpy(pid_program_name,field);
								}


							}
							break;
						}
					case 7:
						{
							//cout << field << endl;

							size += strlen(field)+1;
							if(size < nLen)
							{
								if(ll>38)
								{
									strcpy(path,field);
								}
								else
								{
									strcpy(inode,field);
									//strcpy(path,field);
								}


							}
							break;
						}
						case 8:
						{
							//cout << field << endl;

							size += strlen(field)+1;
							if(size < nLen)
							{
								/*if(ll>53)
								{
									strcpy(pid_program_name,field);
								}
								else*/
								{
									strcpy(pid_program_name,field);
									//strcpy(path,field);
								}


							}
							break;
						}
						case 9:
						{
							//cout << field << endl;

							size += strlen(field)+1;
							if(size < nLen)
							{
								/*if(ll>53)
								{
									strcpy(pid_program_name,field);
								}
								else*/
								{
									strcpy(path,field);
									//strcpy(path,field);
								}


							}
							break;
						}

				
					}
					memset(field, 0, sizeof(field));
					m = 0;
					j++;
				}

			}
              char cUUID[36];
        memset(cUUID,0,36);
        GetUUID(cUUID, sizeof(cUUID));

    

        char *err_msg = NULL;
        char Sql[4096];
        memset(Sql, 0, sizeof(Sql));
        snprintf(Sql, 4096-1, "insert into linux_unix_socket (network_id, protocol, refcnt, flags, types, state, i_node,pid_program_name,path, mac_address, event_task_id) values('%s', '%s', '%s', '%s', '%s', '%s','%s', '%s','%s', '%s', '%s')", 
            cUUID, proc, RefCnt, Flags, Type, state,inode,pid_program_name,path,Mac, eventId);
        int ret = sqlite3_exec(db, Sql, NULL, NULL, &err_msg);
       // cout<<"charu"<<Sql<<endl;
        if(ret != SQLITE_OK)
        {
            perror(err_msg);
            sqlite3_free(err_msg);
        }
         
        }

    }
    fclose(fp);
     system(" sudo rm anp");
}
void GetInterInfo(sqlite3 *db, char *Mac, char *eventId)
{
    system(" sudo netstat -lupe >aup");
    char *fileName = "aup";

    FILE *fp = NULL;
    fp = fopen(fileName, "r");
    if(fp == NULL)
    {
        perror("open netstat file failed");
        return;
    }

    char buf[1024];
    memset(buf, 0, sizeof(buf));

    int len = 0;
    char field[256];
	memset(field,0,256);
	int nLen=4000;
    int size = 0;
    int index=0;
  	while(fgets(buf, 1024-1, fp) != NULL)
	{
        int ll=0;
        index++;
        if(index>2)
        {

       
		len = strlen(buf);
		int j = 0;
		int m = 0;
        char proc[36];
        memset(proc,0,36);
        char rec_q[36];
        memset(rec_q,0,36);
        char send_q[36];
        memset(send_q,0,36);
        char localIP[50];
        memset(localIP,0,50);
        char remoteip[50];
        memset(remoteip,0,50);
        char state[20];
        memset(state,0,20);
        char pid_program_name[256];
        memset(pid_program_name,0,256);
        char username[256];
	    memset(username,0,256);
	    char inode[256];
	    memset(inode,0,256);
		for(int i = 0; i < len; i++)
		{
			if((j == 9|| buf[i] != ' ') && buf[i] != '\n')
			{
				field[m] = buf[i];
				if(m < 255)
					m++;
			}
			else if(buf[i] == ' ' && buf[i-1] == ' ')
			{
				ll++;
				continue;
			}
			else
			{
				switch(j)
				{
				case 0:
					{
						//cout << field << endl;
						size += strlen(field)+1;
						if(size < nLen)
						{
							strcpy(proc,field);
								
						}
						break;
					}
				case 1:
					{
						//cout << field << endl;
						size += strlen(field)+1;
						if(size < nLen)
						{
							strcpy(rec_q,field);
							
						}
						break;
					}
				case 2:
					{
						//cout << field << endl;
						size += strlen(field)+1;
						if(size < nLen)
						{
							strcpy(send_q,field);
							
						}
						break;
					}
				case 3:
					{
						//cout << field << endl;
						size += strlen(field)+1;
						if(size < nLen)
						{
							strcpy(localIP,field);
							
						}
						break;
					}
				case 4:
					{
						//cout << field << endl;
						size += strlen(field)+1;
						if(size < nLen)
						{
							strcpy(remoteip,field);
								
						}
						break;
					}
					case 5:
						{
							//cout << field << endl;
							
							size += strlen(field)+1;
							if(size < nLen)
							{
								if(ll>42)
								{
									strcpy(username,field);
								}
								else
								{
									strcpy(state,field);
								}
								
							
							}
							break;
						}
					case 6:
						{
							//cout << field << endl;

							size += strlen(field)+1;
							if(size < nLen)
							{
								if(ll>47)
								{
									strcpy(inode,field);
								}
								else
								{
									strcpy(username,field);
								}


							}
							break;
						}
					case 7:
						{
							//cout << field << endl;

							size += strlen(field)+1;
							if(size < nLen)
							{
								if(ll>53)
								{
									strcpy(pid_program_name,field);
								}
								else
								{
									strcpy(inode,field);
								}


							}
							break;
						}

					case 8:
						{
							//cout << field << endl;
							
							size += strlen(field)+1;
							if(size < nLen)
							{
								if(ll>59)
								{
									char temp[256];
									memset(temp,0,256);
									strcat(temp,pid_program_name);
									strcat(temp,field);
									memset(pid_program_name,0,256);
									strcpy(pid_program_name,temp);
								}
								else
								{
									strcpy(pid_program_name,field);
								}
														
							}
							break;
						}
					case 9:
						{
							//cout << field << endl;
							size += strlen(field)+1;
							if(size < nLen)
							{
								char Temp[256];
								memset(Temp,0,256);
								strcat(Temp,pid_program_name);
								strcat(Temp,field);
								memset(pid_program_name,0,256);
								strcpy(pid_program_name,Temp);
							
							}
							break;
						}
				}
				memset(field, 0, sizeof(field));
				m = 0;
				j++;
			}

		}
	
        char cUUID[36];
        memset(cUUID,0,36);
        GetUUID(cUUID, sizeof(cUUID));

    

        char *err_msg = NULL;
        char Sql[4096];
        memset(Sql, 0, sizeof(Sql));
        snprintf(Sql, 4096-1, "insert into linux_internet_network (network_id, protocol, recv_q, send_q, local_address, remote_address,state,username,i_node,pid_program_name, mac_address, event_task_id) values('%s', '%s', '%s', '%s', '%s', '%s', '%s','%s', '%s','%s', '%s', '%s')", 
            cUUID, proc, rec_q, send_q, localIP, remoteip, state,username,inode,pid_program_name,Mac, eventId);
        int ret = sqlite3_exec(db, Sql, NULL, NULL, &err_msg);
       // cout<<"charu"<<Sql<<endl;
        if(ret != SQLITE_OK)
        {
            perror(err_msg);
            sqlite3_free(err_msg);
        }
    }
	}
    fclose(fp);
    system(" sudo rm aup");
     system("sudo netstat -ltpe >atp");
    char *fileName1 = "atp";

    FILE *fp1 = NULL;
    fp1 = fopen(fileName1, "r");
    if(fp1 == NULL)
    {
        perror("open netstat file failed");
        return;
    }

    char buf1[1024];
    memset(buf1, 0, sizeof(buf1));

    int len1 = 0;
    char field1[256];
	memset(field1,0,256);
	int nLen1=4000;
    int size1 = 0;
    int index1=0;
  	while(fgets(buf1, 1024-1, fp1) != NULL)
	{
        int ll=0;
        index1++;
        if(index1>2)
        {

        
		len1 = strlen(buf1);
		int j = 0;
		int m = 0;
        char proc[36];
        memset(proc,0,36);
        char rec_q[36];
        memset(rec_q,0,36);
        char send_q[36];
        memset(send_q,0,36);
        char localIP[50];
        memset(localIP,0,50);
        char remoteip[50];
        memset(remoteip,0,50);
        char state[20];
        memset(state,0,20);
        char pid_program_name[256];
        memset(pid_program_name,0,256);
        char username[256];
	    memset(username,0,256);
	    char inode[256];
	    memset(inode,0,256);
		for(int i = 0; i < len; i++)
		{
			if((j == 9 || buf1[i] != ' ') && buf1[i] != '\n')
			{
				field1[m] = buf1[i];
				if(m < 255)
					m++;
			}
			else if(buf1[i] == ' ' && buf1[i-1] == ' ')
			{
				ll++;
				continue;
			}
			else
			{
				switch(j)
				{
				case 0:
					{
						//cout << field << endl;
						size1 += strlen(field1)+1;
						if(size1 < nLen1)
						{
							strcpy(proc,field1);
								
						}
						break;
					}
				case 1:
					{
						//cout << field << endl;
						size1 += strlen(field1)+1;
						if(size1 < nLen1)
						{
							strcpy(rec_q,field1);
							
						}
						break;
					}
				case 2:
					{
						//cout << field << endl;
						size1 += strlen(field1)+1;
						if(size1 < nLen1)
						{
							strcpy(send_q,field1);
							
						}
						break;
					}
				case 3:
					{
						//cout << field << endl;
						size1 += strlen(field1)+1;
						if(size1 < nLen1)
						{
							strcpy(localIP,field1);
							
						}
						break;
					}
				case 4:
					{
						//cout << field << endl;
						size1 += strlen(field1)+1;
						if(size1 < nLen1)
						{
							strcpy(remoteip,field1);
								
						}
						break;
					}
				case 5:
						{
							//cout << field << endl;
							
							size1 += strlen(field1)+1;
							if(size1 < nLen1)
							{
								/*if(ll>42)
								{
									strcpy(username,field1);
								}
								else*/
								{
									strcpy(state,field1);
								}
								
							
							}
							break;
						}
					case 6:
						{
							//cout << field << endl;

							size1 += strlen(field1)+1;
							if(size1 < nLen1)
							{
								/*if(ll>47)
								{
									strcpy(inode,field1);
								}
								else*/
								{
									strcpy(username,field1);
								}


							}
							break;
						}
					case 7:
						{
							//cout << field << endl;

							size1 += strlen(field1)+1;
							if(size1 < nLen1)
							{
								/*if(ll>53)
								{
									strcpy(pid_program_name,field1);
								}
								else*/
								{
									strcpy(inode,field1);
                                    cout<<"inode"<<inode<<endl;
								}


							}
							break;
						}

					case 8:
						{
							//cout << field << endl;
							
							size1 += strlen(field1)+1;
							if(size1 < nLen1)
							{
								/*if(ll>59)
								{
									char temp[256];
									memset(temp,0,256);
									strcat(temp,pid_program_name);
									strcat(temp,field1);
									memset(pid_program_name,0,256);
									strcpy(pid_program_name,temp);
								}
								else*/
								{
									strcpy(pid_program_name,field1);
								}
														
							}
							break;
						}
					case 9:
						{
							//cout << field << endl;
							size1 += strlen(field1)+1;
							if(size1 < nLen1)
							{
								char Temp[256];
								memset(Temp,0,256);
								strcat(Temp,pid_program_name);
								strcat(Temp,field1);
								memset(pid_program_name,0,256);
								strcpy(pid_program_name,Temp);
							
							}
							break;
						}
				}
				memset(field1, 0, sizeof(field1));
				m = 0;
				j++;
			}

		}
	
        char cUUID[36];
        memset(cUUID,0,36);
        GetUUID(cUUID, sizeof(cUUID));

    

        char *err_msg = NULL;
        char Sql[4096];
        memset(Sql, 0, sizeof(Sql));
        snprintf(Sql, 4096-1, "insert into linux_internet_network (network_id, protocol, recv_q, send_q, local_address, remote_address,state,username,i_node,pid_program_name, mac_address, event_task_id) values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s', '%s', '%s')", 
            cUUID, proc, rec_q, send_q, localIP, remoteip, state,username,inode,pid_program_name,Mac, eventId);
        int ret = sqlite3_exec(db, Sql, NULL, NULL, &err_msg);
      //  cout<<"charu"<<Sql<<endl;
        if(ret != SQLITE_OK)
        {
            perror(err_msg);
            sqlite3_free(err_msg);
        }

	}
    }
    fclose(fp1);
     system(" sudo rm atp");
}

// 本地ip
int LocalIp(char *ipbuf, int size)
{
	int i=0;
	int sockfd;
	struct ifconf ifc;
	char buf[1024]={0};
	//char ipbuf[20]={0};
	struct ifreq *ifr;
 
	ifc.ifc_len = 1024;
	ifc.ifc_buf = buf;
 
	if((sockfd = socket(AF_INET, SOCK_DGRAM,0))<0)
	{
	    printf("socket error\n");
		return -1;
	}
	ioctl(sockfd,SIOCGIFCONF, &ifc);
	ifr = (struct ifreq*)buf;
 
	for(i=(ifc.ifc_len/sizeof(struct ifreq)); i > 0; i--)
	{
        if(strcmp(ifr->ifr_name, "lo") == 0)
        {
            ifr = ifr +1;
            continue;
        }
		//printf("net name: %s\n",ifr->ifr_name);
		inet_ntop(AF_INET,&((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr,ipbuf,size);
		//printf("ip: %s \n",ipbuf);
		ifr = ifr +1;
        break;
	}
	return 0;
}

// 系统信息
int HostName(char *buf, int size)
{
    FILE *fp = NULL;
    fp = popen("cat /etc/os-release | grep PRETTY_NAME", "r");
    if(fp == NULL)
    {
        perror("popen error");
        return -1;
    }

    char line[1024];
    memset(line, 0, sizeof(char)*1024);
    if(fgets(line, 1024-1, fp) == NULL)
    {
        pclose(fp);
        return -2;
    }
    //cout << line << endl;
    string str1 = strMid(line, strFind(line, "\""));
    string str2 = StrLeft(str1, strFind(str1.c_str(), "\""));
    strncpy(buf, str2.c_str(), size-1);
    //cout << str2 << endl;
    pclose(fp);

    return 0;
}

// 基础信息
int GetBaseInfo(sqlite3 *db, char *Mac, char *eventId, char *cWebType)
{
    if(db == NULL || Mac == NULL)
        return -1;
    char createTime[64];    // 系统安装时间
    memset(createTime, 0, sizeof(createTime));
    SystemCreateTime(createTime, 64);

    char CurrentTime[64];   // 系统当前时间
    memset(CurrentTime, 0, sizeof(CurrentTime));
    SystemCurrentTime(CurrentTime, 64);

    char Mem[64];           // 内存
    memset(Mem, 0, sizeof(Mem));
    SystemMemory(Mem, 64);

    char disk[64];          //  硬盘大小
    memset(disk, 0, sizeof(disk));
    DiskInfo(disk, 64);
    //cout << strlen(disk) << disk << endl;

    char pInfo[4000];       // 分区信息
    memset(pInfo, 0, sizeof(pInfo));
    PartitionInfo(pInfo, 4000);
    //cout << pInfo << endl;

    char cIp[36];           // 本地ip
    memset(cIp, 0, sizeof(cIp));
    LocalIp(cIp, 36);
  
    //cout << "本地ip = " << cIp << endl;

    char osName[128];       // xitong xinxi 
    memset(osName, 0, sizeof(char)*128);
    HostName(osName, 128);

    char pc_name[128];//zhujiming
    memset(pc_name,0,sizeof(char)*128);
    gethostname(pc_name,128);
    cout << "zhujiming = " << pc_name << endl;
    // UUID
    char cUUID[36];
    GetUUID(cUUID, sizeof(cUUID));

    StrReplace(pInfo, "'", "''");

    char *err_msg = NULL;
    char Sql[4096];
    memset(Sql, 0, sizeof(Sql));
    snprintf(Sql, 4096-1, "insert into Linux_Base_Info (id, disk_size, partition_information, system_installation_time, system_time, memory_size, mac_address, event_task_id) values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", 
        cUUID, disk, pInfo, createTime, CurrentTime, Mem, Mac, eventId);
    int ret = sqlite3_exec(db, Sql, NULL, NULL, &err_msg);
    if(ret != SQLITE_OK)
    {
        perror(err_msg);
        sqlite3_free(err_msg);
    }

    string WebType;
    if(strlen(cWebType) > 0)
        WebType = cWebType;
    else
        WebType = "未找到";
            
    memset(cUUID, 0, sizeof(cUUID));
    GetUUID(cUUID, sizeof(cUUID));
    memset(Sql, 0, sizeof(Sql));
    snprintf(Sql, 4096-1, "insert into Pc_Account(pc_account_id, host_ip, os_name, web_container, install_date, hard_disk_capacity, physical_memory, check_time, mac_address, server_type, event_task_id,pc_name) values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s','%s')", 
        cUUID, cIp, osName, WebType.c_str(),createTime, disk, Mem, CurrentTime, Mac, "linux", eventId,pc_name);
    //cout << Sql << endl;
    ret = sqlite3_exec(db, Sql, NULL, NULL, &err_msg);
    if(ret != SQLITE_OK)
    {
        perror(err_msg);
        sqlite3_free(err_msg);
    }
   
    return 0;
}
